package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.entity.SysCity;
import com.ruoyi.system.mapper.SysCityMapper;
import com.ruoyi.system.service.SysCityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 部门管理 服务实现
 *
 * @author ruoyi
 */
@Service
public class SysCityServiceServiceImpl implements SysCityService {
    @Autowired
    private SysCityMapper cityMapper;

    @Override
    public List<SysCity> selectCityList(Long parentId) {
        return cityMapper.selectCityList(parentId);
    }

    @Override
    public List<TreeSelect> buildCityTreeSelect(List<SysCity> citys) {
        List<SysCity> cityTrees = buildDeptTree(citys);
        return cityTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<Long> selectCityListByUserId(String userId) {
        return cityMapper.selectCityListByUserId(userId);
    }

    @Override
    public List<SysCity> getCityListByUserId(){
        String userId = SecurityUtils.getUserId();
        List<SysCity> list;
        if (SecurityUtils.hasAdmin()) {
            list = selectCityListByParentId(1L);
        } else {
            list = cityMapper.getCityListByUserId(userId);
        }
        return list;
    }

    @Override
    public List<SysCity> selectCityListByParentId(Long parentId){
        List<SysCity> list = cityMapper.selectCityListByParentId(parentId);
        return list;
    }

    @Override
    public List<SysCity> selectCityListFromChildToParentId() {
        String userId = SecurityUtils.getUserId();
        List<SysCity> list;
        if (SecurityUtils.hasAdmin()) {
            list = selectCityListByParentId(1L);
        } else {
            list = cityMapper.selectCityListFromChildToParentId(userId);
        }
        return list;
    }

    /**
     * 构建前端所需要树结构
     *

     */
    private List<SysCity> buildDeptTree(List<SysCity> citys)
    {
        List<SysCity> returnList = new ArrayList<SysCity>();
        List<Long> tempList = new ArrayList<Long>();
        for (SysCity city : citys)
        {
            tempList.add(city.getId());
        }
        for (Iterator<SysCity> iterator = citys.iterator(); iterator.hasNext();)
        {
            SysCity city = (SysCity) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(city.getParentId()))
            {
                recursionFn(citys, city);
                returnList.add(city);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = citys;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysCity> list, SysCity t)
    {
        // 得到子节点列表
        List<SysCity> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysCity tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysCity> list, SysCity t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 得到子节点列表
     */
    private List<SysCity> getChildList(List<SysCity> list, SysCity t)
    {
        List<SysCity> tlist = new ArrayList<SysCity>();
        Iterator<SysCity> it = list.iterator();
        while (it.hasNext())
        {
            SysCity n = (SysCity) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }
}
